﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using MAPIdotnet;
using Microsoft.Win32;
using Microsoft.WindowsMobile.PocketOutlook;
using Terranova.API;

namespace SMSRulez
{
   sealed class Program
    {
        private MAPI mapi;
        private string CONTACT = string.Empty;
        private string MESSAGE = string.Empty;
        private string ACTION = string.Empty;
        private string FOLDER = string.Empty;
        private string AUTORUN = string.Empty;
        private bool Move = true;
        private IMAPIMsgStore[] stores;
        string location = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
        IMAPIFolderID[] subFolders = null;
        IMAPIFolder folder = null;
        IMAPIFolder Inbox = null;
        bool autoMode = false;
        
        
        //DataTable tableRule = new DataTable();
        DataTable tableRule = new DataTable();
        DataTable tableMessage = new DataTable();
        DataTable tableContact = new DataTable();
        string command = string.Empty;
        bool autoRunMe = false;
        List<bool> autoRunMeArray = new List<bool>();
        List<string> folderArray = new List<string>();
        List<string> actionArray = new List<string>();
        //Notification alert = new Notification();
        int countCopy = 0;
        int countMove = 0;
        //int countDelete = 0;
        int countHide = 0;
        int unReadCopy = 0;
        int unReadMove = 0;
        //int unReadDelete = 0;
        int unReadHide = 0;
        
        OutlookSession myOutlook = new OutlookSession();
        SmsAccount SMS = null;
        string smsName = string.Empty;
        string smsOverride = string.Empty;
        bool overrideInbox = false;
        bool hide = false;
        bool scheduled = false;
        string sentFolderName = string.Empty;
        DB db;
        static void Main(string[] args)
        {
            
            Program pg = new Program();
           
            pg.db = new DB(pg.location);
            if (args.Length >= 1)
            {
                if (args[0].Trim().Equals("auto"))
                {
                    pg.autoMode = true;
                    try
                    {
                        IntPtr hOldCursor = Win32.SetCursor(IntPtr.Zero);
                    }
                    catch { }
                }
                else if (args[0].Trim().Equals("overrideInbox"))
                {
                    pg.overrideInbox = true;
                    pg.smsOverride = args[1].Trim();
                }
                else if (args[0].Trim().Equals("sch"))
                {
                    pg.scheduled = true;
                }
            }
            if (pg.scheduled)
            {
                pg.GetSetSchedule(false);
                pg.Setschedule();
            }
            pg.GetSMSCount();
            pg.mapi = new MAPI();
            pg.stores = pg.mapi.MessageStores;
            pg.FillSMSName();
            pg.FillMessageRules();
            pg.FillContactRules();
            pg.GetInboxMessages();
            RegistryKey reg = Registry.CurrentUser.CreateSubKey("Software").CreateSubKey("SmsRule");
            object status = reg.GetValue("notify");
            
            if (status.ToString().Equals("1"))
            {
                //Win32.SendMessage((IntPtr)Win32.HWND_BROADCAST, Win32.WM_CANCELMODE, 0, 0);
                pg.DisplayNotifications();
            }
            try
            {
                pg.KillRun();
            }
            catch { }

            try
            {
                IntPtr hOldCursor = Win32.SetCursor(IntPtr.Zero);
            }
            catch { }
        
        }
        string schTime = "00:00";
        RegistryKey reg = null;
        private void GetSetSchedule(bool set)
        {
           
            reg = Registry.CurrentUser.CreateSubKey("Software").CreateSubKey("SmsRule");
            if (!set)
                schTime = reg.GetValue("schedule").ToString();
            else
                reg.SetValue("schedule", schTime);
        }
        private void Setschedule()
        {
            //schTime = ddlSchHr.SelectedItem.ToString() + ":" + ddlSchMin.SelectedItem.ToString();
            //GetSetSchedule(true);
            int hour = int.Parse(schTime.Split(":".ToCharArray()).GetValue(0).ToString());
            int minute = int.Parse(schTime.Split(":".ToCharArray()).GetValue(1).ToString());
            DateTime scheduledTime = DateTime.Now;

            scheduledTime = scheduledTime.AddDays(1);

            scheduledTime = new DateTime(scheduledTime.Year, scheduledTime.Month, scheduledTime.Day, hour, minute, 00);

            SapScheduler.Scheduler desc = new SapScheduler.Scheduler(location + "\\SMSRules.exe");
            SapScheduler.Scheduler sch = new SapScheduler.Scheduler(scheduledTime, location + "\\SMSRules.exe", "sch");

            if (sch.success)
                reg.SetValue("scheduleSet", 1);
            else
                reg.SetValue("scheduleSet", 0);
        }
        private void RefreshScreen()
        {
            Win32.SendMessage((IntPtr)Win32.HWND_BROADCAST, Win32.WM_WININICHANGE, 0xF2, 0);
            
        }
        private void FindAndKill(string app)
        {
            try
            {
                ProcessCE.FindAndKill(app);
            }
            catch { }
        }


        string originalCount;
        int totalSMSCleaned = 0;
        private void GetSMSCount()
        {
            originalCount = Registry.CurrentUser.OpenSubKey("System").OpenSubKey("State").OpenSubKey("Messages").OpenSubKey("sms").OpenSubKey("Unread").GetValue("Count", 0).ToString();
        }
        private void GetProcessedSMS()
        {
            totalSMSCleaned = unReadCopy +  unReadHide + unReadMove;

        }
        private void SetSMSCount(string Count)
        {
            Registry.CurrentUser.CreateSubKey("System").CreateSubKey("State").CreateSubKey("Messages").CreateSubKey("sms").CreateSubKey("Unread").SetValue("Count", Count, RegistryValueKind.DWord);
            Registry.CurrentUser.CreateSubKey("System").CreateSubKey("State").CreateSubKey("Messages").CreateSubKey("sms").CreateSubKey("Unread").SetValue("Text", "Text Messages (" + Count + ")", RegistryValueKind.String);
            Registry.CurrentUser.Flush();
            RefreshScreen();
        }
        string notifyMessage = string.Empty;
        private void DisplayNotifications()
        {
            RegistryKey reg = Registry.CurrentUser.CreateSubKey("Software").CreateSubKey("SmsRule");
            string txt1 = string.Empty;
            string txt2 = string.Empty;
            object status = reg.GetValue("notifyHide");
            if (!status.ToString().Equals("0"))
            {

                txt2 = "Moved: " + countMove +
                     "</br>Copied: " + countCopy +
                     "</br>Hid: " + countHide;

            }
            else
            {
                txt2 = "Moved: " + countMove +
                       "</br>Copied: " + countCopy;
            }
            SetFilters();
            if (!status.ToString().Equals("0"))
            {
                
                txt1 = "Moved: " + countMove +
                     "</br>Copied: " + countCopy +
                     "</br>Hid: " + countHide;

            }
            else
            {
                txt1 = "Moved: " + countMove +
                       "</br>Copied: " + countCopy;
            }
           string parm1 = @"<form><b>Rules applied, Messages-</b><br />" + txt1 +"</form>";
           string parm2 = @"<form><b>Rules applied, Messages-</b><br />" + txt2 + "</form>";
           
            if (countMove + countCopy + countHide != 0)
            {
                    Process pc = new Process();
                    pc.EnableRaisingEvents = false;
                    pc.StartInfo.UseShellExecute = true;
                    pc.StartInfo.FileName = location + @"\" + "Notification.exe";
                    pc.StartInfo.Arguments = "\""+parm1 + "\" \"" + parm2+"\"";
                    pc.Start();
            }
        }
        private void SetFilters()
        {
            try
            {
                RegistryKey reg = Registry.CurrentUser.CreateSubKey("Software").CreateSubKey("SmsRule").CreateSubKey("SmsFiltered");
                //countDelete += int.Parse(reg.GetValue("Deleted","0").ToString());
                countMove += int.Parse(reg.GetValue("Moved", "0").ToString().Trim());
                countCopy += int.Parse(reg.GetValue("Copied", "0").ToString().Trim());
                countHide += int.Parse(reg.GetValue("Hid", "0").ToString().Trim());

                //reg.SetValue("Deleted", countDelete.ToString());
                reg.SetValue("Moved", countMove.ToString());
                reg.SetValue("Copied", countCopy.ToString());
                reg.SetValue("Hid", countHide.ToString());
                reg.Flush();
            }
            catch { }
        }
        /// <summary>
        /// Fetch all the smses from the SMS inbox folder
        /// </summary>
        private void GetInboxMessages()
        {
            for (int i = 0, length = stores.Length; i < length; i++)
            {
                IMAPIMsgStore store = stores[i];

                if (string.Compare(store.DisplayName.ToUpper(), smsName, StringComparison.OrdinalIgnoreCase | StringComparison.Ordinal) == 0)
                {
                    folder = store.RootFolder.OpenFolder();
                    sentFolderName = store.SentMailFolder.OpenFolder().DisplayName;
                    subFolders = folder.GetSubFolders((int)folder.NumSubFolders);
                    if (overrideInbox)
                    {
                        Inbox = FetchFolderId(smsOverride);
                    }
                    else
                    {
                        Inbox = store.ReceiveFolder.OpenFolder();
                    }
                    List<IMAPIMessage> messages = new List<IMAPIMessage>();
                    if (autoMode)
                    {
                        Inbox.SortMessagesByDeliveryTime(TableSortOrder.TABLE_SORT_DESCEND);
                        //messages = Inbox.GetNextMessages(int.Parse(originalCount) + 2);
                        for (int pos = 0; pos < int.Parse(originalCount) + 2; pos++)
                        {
                            try
                            {
                                //Inbox.SeekMessages(pos);
                                messages.Add((IMAPIMessage)Inbox.GetNextMessages(1).GetValue(0));
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                    }
                    else
                    {
                        Inbox.SortMessagesBySenderName(TableSortOrder.TABLE_SORT_DESCEND);
                        //messages = Inbox.GetNextMessages(Inbox.NumSubItems);
                        for (int pos = 0; pos < Inbox.NumSubItems; pos++)
                        {
                            try
                            {
                                //Inbox.SeekMessages(pos);
                                messages.Add((IMAPIMessage)Inbox.GetNextMessages(1).GetValue(0));
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                    }
                    GetSMS(messages.ToArray());
                    break;
                }
            }
        }
        /// <summary>
        /// Apply rules to each message in the input messageID arrays
        /// </summary>
        /// <param name="msg"></param>
        private void ApplyRules(IMAPIMessageID[] msg)
        {
            HiddenFolderClass hideFolder = new HiddenFolderClass();
            
            IMAPIFolder rulefolder = null;
            List<IMAPIMessageID> oneMessageArray = new List<IMAPIMessageID>();
            string folderName = string.Empty;
            string action = string.Empty;
            bool autoRunMe = false;
            bool skip = false;
            if (autoMode)
            {
                int count = 0;
                foreach (IMAPIMessageID msgID in msg)
                {
                    skip = false;
                    folderName = folderArray[count].Trim();
                    autoRunMe = autoRunMeArray[count];
                    oneMessageArray.Add(msgID);
                    if (autoRunMe)
                    {
                        if (!AssignAction(actionArray[count].Trim()))
                        {
                            rulefolder = FetchFolderId(folderName);
                            if (rulefolder != null)
                            {
                                if (Move)
                                {
                                    if (!msgID.OpenMessage().Flags.Equals(EMessageFlags.MSGFLAG_READ))
                                        unReadMove++;
                                    countMove++;
                                    skip = false;
                                }
                                else
                                {
                                    countCopy++;
                                    if (msgID.OpenMessage().Flags.Equals(EMessageFlags.MSGFLAG_READ))
                                    {
                                        skip = true;
                                    }
                                    else
                                    {
                                        unReadCopy++;
                                        skip = false;
                                        msgID.OpenMessage().Flags = EMessageFlags.MSGFLAG_READ;
                                    }
                                }
                                if (!skip)
                                {
                                    Inbox.CopyMessages(oneMessageArray.ToArray(), rulefolder, false);
                                    try
                                    {
                                        if (Move)
                                            DeleteMessageRule(oneMessageArray.ToArray());
                                    }
                                    catch { }
                                }
                            }
                        }
                        else
                        {
                            if (hide)
                            {
                                countHide++;
                                if (!msgID.OpenMessage().Flags.Equals(EMessageFlags.MSGFLAG_READ))
                                    unReadHide++;
                                Inbox.CopyMessages(oneMessageArray.ToArray(), hideFolder.CreateHiddenFolder(), false);
                                try
                                {
                                    DeleteMessageRule(oneMessageArray.ToArray());
                                }
                                catch { }
                                hide = false;
                            }
                            else
                            {
                                if (!msgID.OpenMessage().Flags.Equals(EMessageFlags.MSGFLAG_READ))
                                    //unReadDelete++;
                                //countDelete++;
                                msgID.OpenMessage().Flags = EMessageFlags.MSGFLAG_READ;
                                DeleteMessageRule(oneMessageArray.ToArray());
                            }
                        }
                    }
                    count++;
                    oneMessageArray.Clear();
                }
            }
            else
            {
                int count = 0;
                foreach (IMAPIMessageID msgID in msg)
                {
                    skip = false;
                    oneMessageArray.Add(msgID);
                    folderName = folderArray[count].Trim();
                    if (!AssignAction(actionArray[count].Trim()))
                    {
                        rulefolder = FetchFolderId(folderName);
                        if (rulefolder != null)
                        {
                            if (Move)
                            {
                                if (!msgID.OpenMessage().Flags.Equals(EMessageFlags.MSGFLAG_READ))
                                    unReadMove++;
                                countMove++;
                                skip = false;
                            }
                            else
                            {
                                countCopy++;
                                if (msgID.OpenMessage().Flags.Equals(EMessageFlags.MSGFLAG_READ))
                                {
                                    skip = true;
                                }
                                else
                                {
                                    unReadCopy++;
                                    skip = false;
                                    msgID.OpenMessage().Flags = EMessageFlags.MSGFLAG_READ;
                                }
                            }
                            if (!skip)
                            {
                                Inbox.CopyMessages(oneMessageArray.ToArray(), rulefolder, /*Move*/false);
                                try
                                {
                                    if(Move)
                                    DeleteMessageRule(oneMessageArray.ToArray());
                                }
                                catch { }
                            }
                        }
                    }
                    else
                    {
                        if (hide)
                        {
                            countHide++;
                            if (!msgID.OpenMessage().Flags.Equals(EMessageFlags.MSGFLAG_READ))
                                unReadHide++;
                            //IMAPIMessage hideMsg = msgID.OpenMessage();
                            Inbox.CopyMessages(oneMessageArray.ToArray(), hideFolder.CreateHiddenFolder(), false);
                            try
                            {
                                DeleteMessageRule(oneMessageArray.ToArray());
                            }
                            catch { }
                            hide = false;
                        }
                        else
                        {
                            if (!msgID.OpenMessage().Flags.Equals(EMessageFlags.MSGFLAG_READ))
                                //unReadDelete++;  
                                //countDelete++;
                                msgID.OpenMessage().Flags = EMessageFlags.MSGFLAG_READ;
                            DeleteMessageRule(oneMessageArray.ToArray());
                        }
                    }
                    oneMessageArray.Clear();
                    count++;
                }
            }

        }

        
        /// <summary>
        /// Get the folder id to use
        /// </summary>
        /// <param name="folderName"></param>
        /// <returns></returns>
        private IMAPIFolder FetchFolderId(string folderName)
        {
            IMAPIFolder rulefolder = null;
            foreach (IMAPIFolderID fldr in subFolders)
            {
                rulefolder = fldr.OpenFolder();
                if (string.CompareOrdinal(rulefolder.DisplayName.ToUpper(), folderName.ToUpper()) == 0)
                {
                    return rulefolder;
                }
            }
            return null;
        }
        
        /// <summary>
        /// Delete the messages
        /// </summary>
        /// <param name="msg"></param>
        private void DeleteMessageRule(IMAPIMessageID[] msg)
        {
            Inbox.DeleteMessages(msg);
        }
        string lastSender = string.Empty;
        bool hasRuleDefined = false;
        /// <summary>
        /// Select messages  from SMS folder which satisfy rules
        /// </summary>
        /// <param name="messages"></param>
        private void GetSMS(IMAPIMessage[] messages)
        {
            List<IMAPIMessageID> ruleMessages = new List<IMAPIMessageID>();
            bool RuleSender = false;
            for (int i = 0, length = messages.Length; i < length; i++)
            {
                RuleSender = false;
                IMAPIMessage msg = messages[i];
                msg.PopulateProperties(EMessageProperties.Sender | EMessageProperties.Subject);
                IMAPIContact sender = msg.Sender;
                if (Inbox.DisplayName.Equals(sentFolderName, StringComparison.OrdinalIgnoreCase))
                {
                    sender = null;
                }
                if (sender == null)
                {
                    try
                    {
                        sender = (IMAPIContact)msg.Recipients.GetValue(0);
                    }
                    catch
                    {
                        try
                        {
                            sender = msg.Sender;
                        }
                        catch
                        {
                            sender = null;
                        }
                    }
                }
               
                if (sender != null)
                {
                    if (hasRuleDefined || !sender.Name.ToString().Trim().Equals(lastSender))
                    {
                        if (ContainsRuleContact(sender.Name.ToString().Trim()))
                        {
                            hasRuleDefined = true;
                            ruleMessages.Add(msg.MessageID);
                            autoRunMeArray.Add(autoRunMe);
                            folderArray.Add(FOLDER);
                            actionArray.Add(ACTION);
                            RuleSender = true;
                        }
                        else
                        {
                            hasRuleDefined = false;
                        }
                    }
                    else
                    {
                        hasRuleDefined = false;
                    }
                    lastSender = sender.Name.ToString().Trim();

                    if (!RuleSender && !string.IsNullOrEmpty(msg.Subject.ToString().Trim()))
                    {
                        if (ContainsRuleMessageText(msg.Subject.ToString().Trim()))
                        {
                            ruleMessages.Add(msg.MessageID);
                            autoRunMeArray.Add(autoRunMe);
                            folderArray.Add(FOLDER);
                            actionArray.Add(ACTION);
                        }
                        RuleSender = false;
                    }
                }
            }
            if (ruleMessages.Count > 0)
            {

               
                ToggleThreadMode();
                ApplyRules(ruleMessages.ToArray());
                ToggleThreadMode();
            }


        }
        bool smsModeChanged = false;
        private void ToggleThreadMode()
        {
            //HKEY_CURRENT_USER\Software\Microsoft\Inbox\Settings\OEM\SMSInboxThreadingDisabled
            try
            {
                RegistryKey reg = Registry.CurrentUser.CreateSubKey("Software").CreateSubKey("Microsoft").CreateSubKey("Inbox").CreateSubKey("Settings").CreateSubKey("OEM");
                if (!smsModeChanged)
                {
                    bool isThreadDisabled = reg.GetValue("SMSInboxThreadingDisabled", "1").ToString() == "1" ? true : false;
                    if (!isThreadDisabled)
                    {
                        while (ProcessCE.IsRunning(@"\Windows\tmail.exe"))
                        {
                            ProcessCE.FindAndKill(@"\Windows\tmail.exe");
                        }
                        reg.SetValue("SMSInboxThreadingDisabled", "1", RegistryValueKind.DWord);
                        smsModeChanged = true;
                        Process.Start(@"\Windows\tmail.exe", "-Service \"SMS\" -RunInBKG");
                    }
                }
                else
                {

                    while (ProcessCE.IsRunning(@"\Windows\tmail.exe"))
                    {
                        ProcessCE.FindAndKill(@"\Windows\tmail.exe");
                    }
                    reg.SetValue("SMSInboxThreadingDisabled", "0", RegistryValueKind.DWord);
                    Process.Start(@"\Windows\tmail.exe", "-Service \"SMS\" -RunInBKG");
                }
                reg.Flush();
            }
            catch(Exception ex) {string mess = ex.Message; }
            RefreshScreen();

        }
        /// <summary>
        /// Check if the specified message validates a contact rule
        /// </summary>
        /// <param name="contact"></param>
        /// <returns></returns>
        private bool ContainsRuleContact(string contact)
        {
            autoRunMe = false;
            contact = contact.ToUpper().Trim();
            ////
            foreach (DataRow contactRow in tableContact.Rows)
            {
                if (contact.Equals(contactRow[1].ToString().ToUpper()))
                {
                    FOLDER = contactRow[0].ToString().Trim();
                    CONTACT = contactRow[1].ToString().ToUpper().Trim();
                    ACTION = contactRow[2].ToString().ToUpper().Trim();
                    AUTORUN = contactRow[3].ToString().ToUpper().Trim();
                    autoRunMe = AUTORUN.Trim().Equals("Y") ? true : false;
                    return true;
                }
                else if (contact.IndexOf(contactRow[1].ToString().ToUpper()) != -1)
                {
                    FOLDER = contactRow[0].ToString().Trim();
                    CONTACT = contactRow[1].ToString().ToUpper().Trim();
                    ACTION = contactRow[2].ToString().ToUpper().Trim();
                    AUTORUN = contactRow[3].ToString().ToUpper().Trim();
                    autoRunMe = AUTORUN.Trim().Equals("Y") ? true : false;
                    return true;
                }
            }
                    CONTACT = string.Empty;
                    FOLDER = string.Empty;
                    ACTION = string.Empty;
                    AUTORUN = string.Empty;
                    return false;
        }   
            ////
            /*
            command = "SELECT * FROM ContactRules WHERE Rule LIKE '" + contact + "'";
            string commandAny = "SELECT * FROM ContactRules WHERE Rule LIKE '%" + contact + "%'";
            SetConnection(command, sqlAction.Select);
            if (tableRule.Rows.Count > 0)
            {
                FOLDER = tableRule.Rows[0][0].ToString().Trim();
                CONTACT = tableRule.Rows[0][1].ToString().ToUpper().Trim();
                ACTION = tableRule.Rows[0][2].ToString().ToUpper().Trim();
                AUTORUN = tableRule.Rows[0][3].ToString().ToUpper().Trim();
                autoRunMe = AUTORUN.Trim().Equals("Y") ? true : false;
                return true;
            }
            else
            {
                SetConnection(commandAny, sqlAction.Select);
                if (tableRule.Rows.Count > 0)
                {
                    FOLDER = tableRule.Rows[0][0].ToString().Trim();
                    CONTACT = tableRule.Rows[0][1].ToString().ToUpper().Trim();
                    ACTION = tableRule.Rows[0][2].ToString().ToUpper().Trim();
                    AUTORUN = tableRule.Rows[0][3].ToString().ToUpper().Trim();
                    autoRunMe = AUTORUN.Trim().Equals("Y") ? true : false;
                    return true;
                }
                else
                {
                    CONTACT = string.Empty;
                    FOLDER = string.Empty;
                    ACTION = string.Empty;
                    AUTORUN = string.Empty;
                    return false;
                }
            }*/
        /// <summary>
        /// Check if the specified message validates a message rule
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        private bool ContainsRuleMessageText(string message)
        {
            autoRunMe = false;
            message = message.ToUpper().Trim();
            foreach (DataRow row in tableMessage.Rows)
            {
                if (message.IndexOf(row[1].ToString().Trim().ToUpper(), StringComparison.Ordinal | StringComparison.OrdinalIgnoreCase) != -1)
                {
                    MESSAGE = row[1].ToString().ToUpper().Trim();
                    FOLDER = row[0].ToString().Trim();
                    ACTION = row[2].ToString().ToUpper().Trim();
                    AUTORUN = row[3].ToString().ToUpper().Trim();
                    autoRunMe = AUTORUN.Trim().Equals("Y") ? true : false;
                    return true;
                }

            }
            return false;
        }
        /// <summary>
        /// Get all message rules
        /// </summary>
        private void FillMessageRules()
        {
            command = "SELECT * FROM MessageRules";
            //using (new Timer("SetConnection", st))
            {
                db.SetConnection(command, DB.sqlAction.Select, out tableRule);
            }
            //using (new Timer("tableMessage", st))
            {
                tableMessage = tableRule.Copy();
            }
        }

        private void FillSMSName()
        {
            SMS = myOutlook.SmsAccount;
            smsName = SMS.Name.Trim().ToUpper();
            myOutlook.Dispose();
            //command = "SELECT smsName FROM SMSFolder";
            //SetConnection(command, sqlAction.Select);
            //smsName = tableRule.Rows[0][0].ToString();
        }

        private void FillContactRules()
        {
            command = "SELECT * FROM ContactRules";
            db.SetConnection(command, DB.sqlAction.Select, out tableRule);
            tableContact = tableRule.Copy();
        }
        /// <summary>
        /// What to do with rules
        /// </summary>
        /// <param name="movedel"></param>
        /// <returns></returns>
        private bool AssignAction(string movedel)
        {
            if (movedel.Equals("MOVE"))
            {
                hide = false;
                Move = true;
                return false;
            }
            else if (movedel.Equals("COPY"))
            {
                hide = false;
                Move = false;
                return false;
            }
            else if (movedel.Equals("DELETE"))
            {
                hide = false;
                return true;
            }
            else if (movedel.Equals("HIDE"))
            {
                hide = true;
                return true;
            }
            else
            {
                Move = true;
                return false;
            }
        }
        /// <summary>
        /// Kill the autolaunch application in exit
        /// </summary>
        private void KillRun()
        {
            string autoLaunchExePath = location + @"\" + "AutoLaunch.exe";
            Process pc = new Process();
            bool killed = false;
            if (ProcessCE.IsRunning(autoLaunchExePath))
            {
                while (!killed)
                {
                    killed = ProcessCE.FindAndKill(autoLaunchExePath);
                }
            }
        }

    }
}